package apis

import (
	"k8sops/pkg/services"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
)

func DealWithPods(c *gin.Context) {
	p, err := services.GetPods("")
	if err != nil {
		log.Fatal(err)
		return
	}

	// 获取异常pod并处理
	pods := make([]string, 0)
	for _, pod := range p.Items {
		if pod.Status.Phase != "Running" {
			// 非Running处理
			pods = append(pods, pod.Name)
		} else {
			for _, containerStatus := range pod.Status.ContainerStatuses {
				if !containerStatus.Ready {
					// Running但容器未Ready处理
					pods = append(pods, pod.Name)
				}
			}
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": pods,
	})
}
